<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 24/10/14
 * Time: 1:09 PM
 */

class Site_Login_Controller extends Base_Controller {

    public $restful = true;

    public function __construct()
    {
        parent::__construct();
        $this->theme->set_theme("default") ;
    }

    public function post_in(){

        $rules = array(
            'username' => 'required|email',
            'password'		=> 'required',
        );
        // Validate all input
        $validator = Validator::make( Input::all(), $rules );

        // Send them back with errors
        if( ! $validator->valid() ){
            return Redirect::to( Url::base() )->with( 'errors', $validator->errors ); // Add errors to the view
        }else{
            $user = array(
                'username' => Input::get('username'),
                'password' => Input::get('password'),
            );

            if (Auth::attempt($user)) {
                if(Auth::user()->role_id == 1){
                    return Redirect::to( '/admin/home' );
                }else{
                    return Redirect::to( '/member' );
                }
            }
        }
    }

    public function get_out(){
        // Destory the session
        Auth::logout();
        // Send them home
        return Redirect::to( URL::base() );
    }


}